const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const path = require('path');

// Swagger配置选项
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'HT Service API',
      version: '1.0.0',
      description: 'HT项目后端API接口文档',
      contact: {
        name: 'API Support',
        email: 'support@example.com'
      }
    },
    servers: [
      {
        url: 'http://localhost:8899', // 修正端口号
        description: '开发环境'
      }
    ],
    components: {
      schemas: {
        User: {
          type: 'object',
          required: ['name', 'email'],
          properties: {
            id: {
              type: 'integer',
              description: '用户ID'
            },
            name: {
              type: 'string',
              description: '用户姓名'
            },
            email: {
              type: 'string',
              format: 'email',
              description: '用户邮箱'
            },
            phone: {
              type: 'string',
              description: '用户电话'
            },
            status: {
              type: 'string',
              enum: ['active', 'inactive'],
              description: '用户状态'
            },
            created_at: {
              type: 'string',
              format: 'date-time',
              description: '创建时间'
            },
            updated_at: {
              type: 'string',
              format: 'date-time',
              description: '更新时间'
            }
          }
        },
        ApiResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              description: '请求是否成功'
            },
            message: {
              type: 'string',
              description: '响应消息'
            },
            data: {
              description: '响应数据'
            }
          }
        },
        LoginRequest: {
          type: 'object',
          required: ['username', 'password'],
          properties: {
            username: {
              type: 'string',
              description: '用户名'
            },
            password: {
              type: 'string',
              description: '密码'
            }
          }
        },
        RegisterRequest: {
          type: 'object',
          required: ['username', 'password', 'email'],
          properties: {
            username: {
              type: 'string',
              description: '用户名'
            },
            password: {
              type: 'string',
              description: '密码'
            },
            email: {
              type: 'string',
              format: 'email',
              description: '邮箱'
            }
          }
        }
      }
    }
  },
  // 修复路径问题
  apis: [
    path.join(__dirname, '../routes/users.js'),
    path.join(__dirname, '../routes/auth.js'),
    path.join(__dirname, '../routes/documents.js'),
    path.join(__dirname, '../routes/contracts.js'), // 新增合同路由
    path.join(__dirname, '../routes/payments.js') // 新增支付路由
  ]
};

try {
  // 生成Swagger规范
  const specs = swaggerJSDoc(options);
  
  // 添加调试信息
  console.log('📚 Swagger配置加载完成');
  console.log('📁 扫描路径:', options.apis);
  console.log('📄 找到的API数量:', Object.keys(specs.paths || {}).length);
  
  if (Object.keys(specs.paths || {}).length === 0) {
    console.warn('⚠️ 警告: 没有找到任何API接口，请检查路由文件中的Swagger注释');
  }
  
  module.exports = {
    swaggerUi,
    specs
  };
} catch (error) {
  console.error('❌ Swagger配置错误:', error.message);
  
  // 提供一个基本的fallback配置
  const fallbackSpecs = {
    openapi: '3.0.0',
    info: {
      title: 'HT Service API',
      version: '1.0.0',
      description: 'API文档加载失败，请检查配置'
    },
    paths: {}
  };
  
  module.exports = {
    swaggerUi,
    specs: fallbackSpecs
  };
}